package com.example.mybatisplusdemo.service;

import com.example.mybatisplusdemo.mapper.BookingTrackMapper;
import com.example.mybatisplusdemo.model.domain.BookingTrack;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.UUID;

@Service
@RequiredArgsConstructor
public class BookingTrackService {

    private final BookingTrackMapper bookingTrackMapper;

    /**
            * 创建预订跟踪记录
     */
    public String createTracking(Long flightId, Long userId, String supplier) {
        String trackingId = "TRK" + UUID.randomUUID().toString().replace("-", "").substring(0, 15).toUpperCase();

        BookingTrack track = new BookingTrack();
        track.setFlightId(flightId);
        track.setUserId(userId);
        track.setSupplier(supplier);
        track.setTrackingId(trackingId);
        track.setStatus("INITIATED");
        track.setCreateTime(LocalDateTime.now());

        bookingTrackMapper.insert(track);

        return trackingId;
    }

    /**
            * 生成跳转链接
     */
    public String generateRedirectUrl(Long flightId, String supplier, String trackingId) {
        // 实际项目中这里应该查询航班信息
        String flightNumber = "CA1501"; // 简化处理

        String baseUrl = "";
        switch (supplier) {
            case "携程": baseUrl = "https://ctrip.com"; break;
            case "去哪": baseUrl = "https://qunar.com"; break;
            case "飞猪": baseUrl = "https://fliggy.com"; break;
            case "美团": baseUrl = "https://meituan.com"; break;
        }

        return baseUrl + "/booking?flight=" + flightNumber + "&trackingId=" + trackingId;
    }
}
